package com.xw.childhealth.controller;

import com.xw.childhealth.VO.DataDictionaryVO;
import com.xw.childhealth.VO.ResultVO;
import com.xw.childhealth.entity.XzCodeMaintain;
import com.xw.childhealth.service.XzCodeMaintainService;
import com.xw.childhealth.util.ResultVOUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @Description:
 * @Author: y
 * @CreateDate: 2019-11-18 08:27
 * @Version: 1.0
 */
@RestController
@RequestMapping("/xzCodeMaintain")
@Slf4j
public class XzCodeMaintainController {

    @Autowired
    private XzCodeMaintainService xzCodeMaintainService;


    @PostMapping("/getXzCodeMaintain")
    public ResultVO getXzCodeMaintain() {

        List<DataDictionaryVO> dataDictionaryVOList = new ArrayList<>();

        List<DataDictionaryVO> list = recursionData(dataDictionaryVOList, 0);

        return ResultVOUtil.success(list);
    }

    /**
     * 查询父节点下面的子节点信息
     *
     * @param nodeId
     * @return
     */
    @PostMapping("/getNodeInfo/{nodeId}")
    public ResultVO getNodeInfo(@PathVariable Integer nodeId) {
        List<XzCodeMaintain> result = xzCodeMaintainService.findParentNode(nodeId);
        return ResultVOUtil.success(result);
    }


    @PostMapping("/findAreaNameFullIn")
    public ResultVO findAreaNameFullIn(@RequestParam("str") String str) {
        List<XzCodeMaintain> like = xzCodeMaintainService.findByAreaNameFullLike("%" + str + "%");
        return ResultVOUtil.success(like);
    }


    /**
     * 查找下级节点目录
     *
     * @param dataDictionaryVOList 数据字典 List
     * @param id                   查找的上级 id
     * @return
     */
    private List<DataDictionaryVO> recursionData(List<DataDictionaryVO> dataDictionaryVOList, Integer id) {

        List<XzCodeMaintain> parentNode = xzCodeMaintainService.findParentNode(id);
        if (parentNode == null || parentNode.isEmpty()) {
            return dataDictionaryVOList;
        }
        parentNode.forEach(e -> {
            DataDictionaryVO dataDictionaryVO = new DataDictionaryVO();
            dataDictionaryVO.setId(e.getId());
            dataDictionaryVO.setTitle(e.getInformation());

            if (e.getInformation() != null) {
                log.trace("");
                List<DataDictionaryVO> list = recursionData(new ArrayList<>(), e.getId());

                dataDictionaryVO.setChildren(list.size() == 0 ? null : list);

                dataDictionaryVOList.add(dataDictionaryVO);
            }

        });

        return dataDictionaryVOList;
    }


    /**
     * 修改或者添加
     *
     * @param map
     * @return
     */
    @RequestMapping("/data-upd")
    public ModelAndView edit(@RequestParam(value = "type", required = false) String type, @RequestParam(value = "nodeId", required = false) Integer nodeId,
                             @RequestParam(value = "xzdm", required = false) String xzdm,
                             @RequestParam(value = "bm", required = false) String bm,
                             @RequestParam(value = "dqjc", required = false) String dqjc,
                             @RequestParam(value = "dqqc", required = false) String dqqc,
                             @RequestParam(value = "cxfl", required = false) String cxfl,
                             @RequestParam(value = "category", required = false) String category,
                             @RequestParam(value = "dqmcqm", required = false) String dqmcqm,
                             @RequestParam(value = "xzjb", required = false) Integer xzjb,
                             @RequestParam(value = "ty", required = false) String ty,
                             @RequestParam(value = "addRecordNode", required = false) Integer addRecordNode, Map<String, Object> map) {
        if (type == null) {
            return new ModelAndView("/pages/admin/xz-code-maintain", map);
        }
        if (type.equals("toUpd")) {
            XzCodeMaintain one = xzCodeMaintainService.findOneById(nodeId);
            map.put("type", type);
            map.put("result", one);
        } else if (type.equals("upd")) {
            XzCodeMaintain one = xzCodeMaintainService.findOneById(nodeId);
            xzCodeMaintainService.save(one.setXzCode(xzdm).setCode(bm).setAreaSimple(dqjc).setAreaFull(dqqc).setCxCategory(cxfl).setCategory(category).setXzGrade(xzjb).setAreaNameFull(dqmcqm).setUpdateTime(new Date()).setIsOpen(ty == null ? 0 : 1));
            return new ModelAndView("/pages/admin/xz-code-maintain", map);
        }
        if (type.equals("toAdd")) {
            map.put("addRecordNode", addRecordNode);
        } else if (type.equals("add")) {
            xzCodeMaintainService.save(new XzCodeMaintain().setXzCode(xzdm).setCode(bm).setAreaSimple(dqjc).setAreaFull(dqqc).setCxCategory(cxfl).setCategory(category).setXzGrade(xzjb).setAreaNameFull(dqmcqm).setCreateTime(new Date()).setUpdateTime(new Date()).setParentId(addRecordNode).setIsOpen(ty == null ? 0 : 1));
            return new ModelAndView("/pages/admin/xz-code-maintain", map);
        }
        return new ModelAndView("/pages/admin/xz-code-maintain-data-add", map);
    }


    /**
     * 查找下级节点目录
     */
//    private List<DataDictionaryVO> recursionData(List<DataDictionaryVO> dataDictionaryVOList, String id) {
//
//        String host = "https://api02.aliyun.venuscn.com";
//        String path = "/area/query";
//        String method = "GET";
//        String appcode = "595ed8e59f1e413ea5b4d3c2f1fe541e";
//        Map<String, String> headers = new HashMap<String, String>();
//        //最后在header中的格式(中间是英文空格)为Authorization:APPCODE 83359fd73fe94948385f570e3c139105
//        headers.put("Authorization", "APPCODE " + appcode);
//        Map<String, String> querys = new HashMap<String, String>();
//        querys.put("level", "0");
//        querys.put("page", "1");
//        querys.put("size", "50");
//        querys.put("parent_id", id);
//
//        try {
//            HttpResponse response = HttpUtils.doGet(host, path, method, headers, querys);
//            //获取response的body
////            System.out.println(EntityUtils.toString(response.getEntity()));
//            Response2EntityBeanVO convert = Response2Entity.convert(EntityUtils.toString(response.getEntity()));
//            if (convert.getMsg().equals("success")) {
//                convert.getData().forEach(e -> {
//                    DataDictionaryVO dataDictionaryVO = new DataDictionaryVO();
//                    dataDictionaryVO.setId(Integer.valueOf(e.getId()));
//                    dataDictionaryVO.setTitle(e.getName());
//                    List<DataDictionaryVO> list = recursionData(new ArrayList<>(), e.getId());
//                    dataDictionaryVO.setChildren(list.size() == 0 ? null : list);
//                    dataDictionaryVOList.add(dataDictionaryVO);
//                });
//            }
//
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
//
//        return dataDictionaryVOList;
//    }
    @RequestMapping("/data-del")
    public ModelAndView del(@RequestParam("id") Integer id) {
        XzCodeMaintain result = xzCodeMaintainService.findOneById(id);
        if (result != null) {
            xzCodeMaintainService.delRecord(id);
        }
        return new ModelAndView("/pages/admin/xz-code-maintain");
    }
}
